/*
 * FramCarto.java
 *
 * Created on 19 juillet 2001, 23:51
 */
// Main-Class=sma.carto.FramCarto
package sma.carto;

import java.awt.FileDialog;
import java.io.File;
import java.io.RandomAccessFile;

/**
 *
 * @author  MARSOLLE
 */
public class FramCarto extends java.awt.Frame {
  private String direct = "D:\\GPS\\byo\\64O\\";
  
    /** Creates new form FramCarto */
  public FramCarto() {
    initComponents ();
    pack ();
  }
  
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
  private void initComponents() {//GEN-BEGIN:initComponents
    tfRep = new java.awt.TextField();
    pbLire = new java.awt.Button();
    tfCode = new java.awt.TextField();
    pbEcrire = new java.awt.Button();
    taLog = new java.awt.TextArea();
    setLayout(new java.awt.GridBagLayout());
    java.awt.GridBagConstraints gridBagConstraints1;
    setBackground(java.awt.SystemColor.control);
    setTitle("CartoByte");
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        exitForm(evt);
      }
    }
    );
    
    tfRep.setBackground(java.awt.Color.white);
    tfRep.setFont(new java.awt.Font ("Dialog", 0, 11));
    tfRep.setColumns(50);
    tfRep.setForeground(java.awt.Color.black);
    tfRep.setText("chemin");
    
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 0;
    gridBagConstraints1.gridy = 0;
    gridBagConstraints1.gridwidth = 2;
    add(tfRep, gridBagConstraints1);
    
    
    pbLire.setFont(new java.awt.Font ("Dialog", 0, 11));
    pbLire.setLabel("Lire un code...");
    pbLire.setBackground(new java.awt.Color (224, 204, 152));
    pbLire.setForeground(java.awt.Color.black);
    pbLire.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        button1ActionPerformed(evt);
      }
    }
    );
    
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 2;
    gridBagConstraints1.gridy = 0;
    add(pbLire, gridBagConstraints1);
    
    
    tfCode.setBackground(java.awt.Color.white);
    tfCode.setFont(new java.awt.Font ("Dialog", 0, 11));
    tfCode.setColumns(10);
    tfCode.setForeground(java.awt.Color.black);
    tfCode.setText("code");
    
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 0;
    gridBagConstraints1.gridy = 1;
    gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
    gridBagConstraints1.weightx = 4.0;
    add(tfCode, gridBagConstraints1);
    
    
    pbEcrire.setFont(new java.awt.Font ("Dialog", 0, 11));
    pbEcrire.setLabel("Ecrire code dans fichiers r\u00e9pertoire...");
    pbEcrire.setBackground(new java.awt.Color (224, 204, 152));
    pbEcrire.setForeground(java.awt.Color.black);
    pbEcrire.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        pbEcrireActionPerformed(evt);
      }
    }
    );
    
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 1;
    gridBagConstraints1.gridy = 1;
    gridBagConstraints1.gridwidth = 2;
    add(pbEcrire, gridBagConstraints1);
    
    
    taLog.setBackground(new java.awt.Color (224, 204, 152));
    taLog.setEditable(false);
    taLog.setFont(new java.awt.Font ("Dialog", 0, 11));
    taLog.setForeground(java.awt.Color.black);
    taLog.setText("Lire un code dans un byo d\u00e9j\u00e0 install\u00e9\npuis \u00e9crire le code dans tous les byo d'un r\u00e9pertoire.\n(attention le code est sp\u00e9cifique \u00e0 un disque dure)");
    
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 0;
    gridBagConstraints1.gridy = 2;
    gridBagConstraints1.gridwidth = 3;
    gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints1.weighty = 5.0;
    add(taLog, gridBagConstraints1);
    
  }//GEN-END:initComponents
  
  private void pbEcrireActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbEcrireActionPerformed
    // Add your handling code here:
    
    // pour s�lectionner le r�p cible
    changeRep();
    
    File repertoire;
    String[] listFich;
    repertoire = new File(direct);
    
    listFich = repertoire.list();
    //FilenameFilter
    
    RandomAccessFile raf;
    int clei; // cl� int
    // conversion hexa => int de la cl�
    clei = (int) Long.parseLong(tfCode.getText() , 16);
    //clei = Integer.parseInt("adefde2" , 16);
    System.out.println( "Code : " + clei);
    
    // pour chaque fichier du r�pertoire
    for (int i=0 ; i < listFich.length ; i++) {
      //listFich[i].
      if ( listFich[i].endsWith(".byo") ) {
        log( listFich[i] +"...  ");
        
        try {
          raf = new RandomAccessFile(direct + listFich[i] , "rw");
          raf.seek( Long.parseLong("0188", 16) );
          raf.writeInt(clei);
          raf.close();
          //log( "OK");
          
        } catch (Exception e) {
          log(e.toString() );
        }
      }
    }
    
  }//GEN-LAST:event_pbEcrireActionPerformed
  
  private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
    // Add your handling code here:
    tfRep.setText("?");
    taLog.setText("");
    FileDialog fd = changeRep();
    RandomAccessFile raf;
    int clei=0;
    // lecture de cl�
    try {
      System.out.println(direct + " " + fd.getFile());
      raf = new RandomAccessFile(direct + fd.getFile() , "r");
      raf.seek( Long.parseLong("0188", 16) );
      clei = raf.readInt();
      raf.close();
      
      log("Code :" + clei + "  Hex : " + Integer.toHexString(clei));
      
    } catch (Exception e) {
      log(e.toString());
    }
    
    tfCode.setText( Integer.toHexString(clei) );
    
  }//GEN-LAST:event_button1ActionPerformed
  
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
      System.exit (0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main (String args[]) {
      new FramCarto ().show ();
    }
    
    // pour logguer les message dans la text area
    public void log(String message) {
      taLog.append(message + "\n");
      System.out.println(message);
    }
    
    private FileDialog changeRep() {
      FileDialog fd = new  FileDialog(this);
      fd.setDirectory( direct );
      fd.setFile("*.byo");
      fd.show();
      direct = fd.getDirectory();
      tfRep.setText( direct );
      return fd;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextField tfRep;
    private java.awt.Button pbLire;
    private java.awt.TextField tfCode;
    private java.awt.Button pbEcrire;
    private java.awt.TextArea taLog;
    // End of variables declaration//GEN-END:variables
    
}
